package com.miyuan.view.home;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.miyuan.R;
import com.miyuan.base.BaseFragment;
import com.miyuan.bean.ResultBean;
import com.miyuan.bean.request.MachineListParam;
import com.miyuan.bean.respone.MachineListBean;
import com.miyuan.bean.respone.UserBean;
import com.miyuan.net.NetService;
import com.miyuan.net.RetrofitClient;
import com.miyuan.utils.SPUtils;
import com.miyuan.view.mine.AboutUsActivity;
import com.miyuan.view.mine.ModifyProfileActivity;
import com.miyuan.view.mine.RecruitRecordActivity;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UserFragment extends BaseFragment {
    @BindView(R.id.about)
    LinearLayout about_layout;
    @BindView(R.id.goods_list)
    LinearLayout goods_list;
    @BindView(R.id.user_content_root)
    LinearLayout user_root;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_content)
    TextView userContent;
    @BindView(R.id.user_tel)
    TextView userTel;


    //构造函数
    public static UserFragment newInstance() {
        UserFragment userFragment = new UserFragment();
        userFragment.setArguments(new Bundle());
        return userFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setFragmentLayout(R.layout.fragment_user);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RetrofitClient.getInstance()
                .create(NetService.class)
                .getUserInfo()
                .enqueue(new Callback<ResultBean<UserBean>>() {


                    @Override
                    public void onResponse(Call<ResultBean<UserBean>> call, Response<ResultBean<UserBean>> response) {
                        ResultBean<UserBean> resultBean = response.body();
                        if (resultBean.getData() != null && userName != null && userContent != null && userTel != null) {
                            UserBean userBean = resultBean.getData();
                            userName.setText(userBean.getName());
                            userContent.setText("账户：" + userBean.getAccount());
                            userTel.setText("联系电话：" + userBean.getPhone());
                        } else {
                            Toast.makeText(getActivity(), resultBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultBean<UserBean>> call, Throwable t) {

                    }

                });
    }

    @OnClick(R.id.about)
    void clickAbout(View view) {
        startActivity(new Intent(getActivity(), AboutUsActivity.class));
    }

    @OnClick(R.id.goods_list)
    void clickGoodsList(View view) {
        startActivity(new Intent(getActivity(), RecruitRecordActivity.class));
    }

    @OnClick(R.id.user_content_root)
    void jump() {
//        startActivity(new Intent(getActivity(), ModifyProfileActivity.class));
    }

    @OnClick(R.id.login_out)
    void loginOut() {
        SPUtils.getInstance().remove("Authorization", true);
//        Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
//        PendingIntent restartIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
//        AlarmManager mgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
//        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent);
//        System.exit(0);
        Intent i = getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        getActivity().finish();
    }
}
